package com.syni.mdd.sjb.common.datatreaure.controller.api;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mdd.sjb.common.dxshapp.CommonPushToDsxhAppDto;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.sjb.common.datatreasure.DxshApiNewsRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.NewDetailParamsRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.BaseRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.apiAppNews.SaasUnifiedPushVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @className DxshApiNewsController
 * @description TOO
 * @Author cfx
 * @DATE 2020/5/4 17:54
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/apiAppNews")
@Log4j2
public class DxshApiNewsRestApi implements DxshApiNewsRestService{

    @Autowired
    DxshAppNewsService dxshAppNewsService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @PostMapping("/commonPushToDxshApp")
    @ApiOperation(value = "推送通知到电享生活平台")
    public RpcResponseVo commonPushToDxshApp(@RequestBody @Validated CommonPushToDsxhAppDto commonPushToDsxhAppDto){
        return dxshAppNewsService.commonPushToDsxhApp(commonPushToDsxhAppDto);
    }

    @PostMapping("/commonPushToDxshApp/v2")
    @ApiOperation(value = "推送通知到电享生活平台")
    public RpcResponseVo commonPushToDxshApp_v2(@RequestBody @Validated CommonPushToDsxhAppDto commonPushToDsxhAppDto){
        return dxshAppNewsService.commonPushToDsxhApp_v2(commonPushToDsxhAppDto);
    }


    @PostMapping("/saasUnifiedPush")
    @ApiOperation(value = "统一消息推送")
    public Map<String,Object> saasUnifiedPush(@RequestBody @Validated SaasUnifiedPushVo saasUnifiedPushVo){
        log.info("saasUnifiedPush接收的参数：{}", JSON.toJSONString(saasUnifiedPushVo));
        try {
            if(StringUtils.isNotBlank(saasUnifiedPushVo.getData().getAppid())){
                if(saasUnifiedPushVo.getMsgType() == SaasUnifiedPushVo.MSG_TYPE_DADAN
                        && saasUnifiedPushVo.getSign() == SaasUnifiedPushVo.MSG_TYPE_DADAN_FAIL) {
                    DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getByAppId(saasUnifiedPushVo.getData().getAppid());
                    if (dxshAppUserChannel != null) {
                        CommonPushToDsxhAppDto commonPushToDsxhAppDto = new CommonPushToDsxhAppDto();
                        commonPushToDsxhAppDto.setCode(String.valueOf(NewDetailParamsRpVo.APPLET_FAILED_PRINT_CODE));
                        commonPushToDsxhAppDto.setAlert("小程序打单失败");
                        commonPushToDsxhAppDto.setUserId(dxshAppUserChannel.getUserId());
                        commonPushToDsxhAppDto.setDescribes("小程序打单提醒");
                        commonPushToDsxhAppDto.setMainTitle("订单");
                        commonPushToDsxhAppDto.setMessageType(Constants.DANK_ORDER_STATUS);
                        commonPushToDsxhAppDto.setMessageSecondType(Constants.DANK_ORDER_STATUS_MINAPP);
                        commonPushToDsxhAppDto.setPhone(saasUnifiedPushVo.getData().getSysUserPhone());
                        commonPushToDsxhAppDto.setStoreId(dxshAppUserChannel.getStoreId());
                        commonPushToDsxhAppDto.setBusinessId(dxshAppUserChannel.getBusinessId());
                        dxshAppNewsService.commonPushToDsxhAppHandler(commonPushToDsxhAppDto);
                    }
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return ResultMapHelper.success(ResultMsg.HAND_SUCCESS);
    }


    @PostMapping("/tset")
    @ApiOperation(value = "推送通知到平台")
    public Map<String,Object> test(@RequestBody @Validated BaseRqVo baseRqVo){
        return dxshAppNewsService.test(baseRqVo);
    }



}
